/*普通登录 vs 短信登录 js*/
function change(e) {

    var zhlogin = document.getElementById("zhlogin");
    var dxlogin = document.getElementById("dxlogin");

    var mima = document.getElementById("mima");
    var dxma = document.getElementById("dxma");
    if (e == 1) {
        zhlogin.className = 'fl login-page-a login-page-black';
        dxlogin.className = 'fl login-page-a';
        dxma.style.display = 'none';
        mima.style.display = 'block';


    } else if (e == 2) {
        zhlogin.className = 'fl login-page-a';
        dxlogin.className = 'fl login-page-a login-page-black';
        dxma.style.display = 'block';
        mima.style.display = 'none';
    }
}

/*普通登录 vs 短信登录 jquery*/
// $("#zhlogin").click(function () {
//     $("#zhlogin").addClass("login-page-black");
//     $("#dxlogin").removeClass("login-page-black");
//     $("#mima").show();
//     $("#dxma").hide();
// });
// $("#dxlogin").click(function () {
//     $("#dxlogin").addClass("login-page-black");
//     $("#zhlogin").removeClass("login-page-black");
//     $("#dxma").show();
//     $("#mima").hide();
// });
window.onload = function(){
    var loginForm = document.getElementById('loginForm');
    var loginPhone = document.getElementById('loginPhone');
    var loginPassword = document.getElementById('loginPassword');
    var remember = document.getElementById('remember');

    //页面初始化时，如果帐号密码cookie存在则填充
    if(getCookie('loginPhone') && getCookie('loginPassword')){
        loginPhone.value = getCookie('loginPhone');
        loginPassword.value = getCookie('loginPassword');
        remember.checked = true;
    }
    //复选框勾选状态发生改变时，如果未勾选则清除cookie
    remember.onchange = function(){
        if(!this.checked){
            delCookie('loginPhone');
            delCookie('loginPassword');
        }
    };
    //表单提交事件触发时，如果复选框是勾选状态则保存cookie
    loginForm.onsubmit = function(){
        if(remember.checked){
            setCookie('loginPhone',loginPhone.value,1); //保存帐号到cookie，有效期7天
            setCookie('loginPassword',loginPassword.value,1); //保存密码到cookie，有效期7天
        }
    };
};

//设置cookie
function setCookie(name,value,day){
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires='+ date;
};

//获取cookie
function getCookie(name){
    var reg = RegExp(name+'=([^;]+)');
    var arr = document.cookie.match(reg);
    if(arr){
        return arr[1];
    }else{
        return '';
    }
};

function delCookie(name){
    setCookie(name,null,-1);
}


function checkLoginName(){
    var loginPhone = document.getElementById("loginPhone").value;
    var phoneRegex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    document.getElementById("loginPhone").placeholder = '请输入手机号';
    if(loginPhone.length==0){
        document.getElementById("error-tip").innerText= "手机号不能为空";
        document.getElementById("loginButton").style.color = "white";
        document.getElementById("loginButton").disabled = true;
    }else{
        if(!(phoneRegex.test(loginPhone))) {
            document.getElementById("error-tip").innerText = "手机号格式错误";
        }else {
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else {
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }

            var url = "/client/isUserRegister?loginPhone="+loginPhone;
            xhr.open("GET", url, true);
            xhr.onreadystatechange = processLoginIsRegister;
            xhr.send();
        }
    }
}

function processLoginIsRegister() {
    if(xhr.readyState == 4 && xhr.status == 200) {
        var result = JSON.parse(xhr.responseText);
        if(result == 0){
            document.getElementById("error-tip").innerText = "手机号还未注册";
            document.getElementById("loginButton").style.color = "white";
            document.getElementById("loginButton").disabled = true;
        }else{
            document.getElementById("error-tip").innerText = null;
            document.getElementById("loginButton").style.color = "white";
            document.getElementById("loginButton").disabled = false;
        }
    }
}

function checkLoginPwd() {
    var loginPassword = document.getElementById("loginPassword").value;
    document.getElementById("loginPassword").placeholder='请输入您的密码'
    if(loginPassword.length!=0){
        document.getElementById("loginButton").style.color = "white";
        document.getElementById("error-tip1").innerText= null;
    }
}


var InterValObj; //timer变量，控制时间
var count=59;
var curCount;

//检查获得验证码按钮()
function getLoginVerificationCode() {
    var loginPhone = document.getElementById("loginPhone").value;
    var phoneRegex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;

    if(loginPhone.length==0){
        document.getElementById("sendButton").style.color="white";
        document.getElementById("sendButton").disabled=true;
        document.getElementById("sendButton").style.cursor="auto";
        document.getElementById("error-tip1").innerText="手机号不能为空";
    }else{
        if(!(phoneRegex.test(loginPhone))) {
            document.getElementById("sendButton").style.color="white";
            document.getElementById("sendButton").disabled=true;
            document.getElementById("sendButton").style.cursor="auto";
            document.getElementById("error-tip1").innerText = "手机号格式错误";
        }else{
            document.getElementById("error-tip1").innerText = null;

            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else{
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }

            curCount=count;
            document.getElementById("sendButton").style.color="white";
            document.getElementById("sendButton").disabled = true;
            document.getElementById("sendButton").style.cursor="wait";
            document.getElementById("sendButton").value=curCount+"s重新发送";
            InterValObj=window.setInterval(SetRemainTime,1000);
            var url = "/client/checkLoginVerificationCode?loginPhone="+loginPhone;
            xhr.open("GET",url,true);
            xhr.send();
        }
    }
}

//设置时间
function SetRemainTime() {
    if(curCount==1){
        window.clearInterval(InterValObj);
        document.getElementById("sendButton").style.color="black";
        document.getElementById("sendButton").value="发送验证码";
        document.getElementById("sendButton").disabled = false;
        document.getElementById("sendButton").style.cursor="pointer";

        document.getElementById("loginButton").disabled=true;
        document.getElementById("loginButton").style.cursor="auto";
    }else {
        --curCount;
        document.getElementById("sendButton").style.color="black";
        document.getElementById("sendButton").value=curCount+"s重新发送";
        document.getElementById("loginButton").disabled=false;
        document.getElementById("loginButton").style.cursor="pointer";
    }
}



function checkLoginButton() {
    var loginPhone = document.getElementById("loginPhone").value;
    var loginPassword = document.getElementById("loginPassword").value;
    // var verificationCode = document.getElementById("loginVerificationCode").value;

    if(loginPhone.length==0){
        document.getElementById("loginButton").style.color = "white";
        document.getElementById("error-tip").innerText= "手机号不能为空";
        document.getElementById("loginButton").disabled = true;
        document.getElementById("loginButton").style.cursor="wait";
    }

    if(loginPassword.length==0){
        document.getElementById("loginButton").style.color = "white";
        document.getElementById("error-tip1").innerText= "密码不能为空";
        document.getElementById("loginButton").style.cursor="wait";

    }
    // if(verificationCode.length==0){
    //     document.getElementById("loginButton").disabled = true;
    //     document.getElementById("error-tip3").innerText = "验证码不能为空";
    //     document.getElementById("loginButton").style.color="black";
    // }
}


// layui.use(['form', 'layedit', 'laydate'], function () {
//     var form = layui.form
//         , layer = layui.layer
//         , layedit = layui.layedit
//         , laydate = layui.laydate;
//
//
//     //自定义验证规则
//     form.verify({
//         pass: [
//             /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
//             , '密码至少包含 数字和英文，长度6-20'
//         ]
//         , content: function (value) {
//             layedit.sync(editIndex);
//         }
//     });
//
//     // //监听指定开关
//     // form.on('switch(switchTest)', function (data) {
//     //     layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
//     //         offset: '6px'
//     //     });
//     //     layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
//     // });
//
//     // //监听提交
//     // form.on('submit(demo1)', function (data) {
//     //     layer.alert(JSON.stringify(data.field), {
//     //         title: '最终的提交信息'
//     //     })
//     //     return false;
//     // });
//
//
//     // //表单取值
//     // layui.$('#LAY-component-form-getval').on('click', function () {
//     //     var data = form.val('example');
//     //     alert(JSON.stringify(data));
//     // });
//
// });